<?php
require_once APP . '/vendors/twitteroauth/twitterOAuth.php';

App::import('Component', 'BAuth');

class TwitterAuthComponent extends BAuthComponent {
  
  /*
#  Consumer key
TugtiAax9rdLKVASofVe3Q
# Consumer secret
W7MMYuN7kFL1mB9hjnjKfOl3J0353AXWz4hF6GztUXw
# Request token URL
https://twitter.com/oauth/request_token
# Access token URL
https://twitter.com/oauth/access_token
# Authorize URL
https://twitter.com/oauth/authorize
  */

  var $consumer_key = 'TugtiAax9rdLKVASofVe3Q';
  var $consumer_secret = 'W7MMYuN7kFL1mB9hjnjKfOl3J0353AXWz4hF6GztUXw';
  var $request_token_url = 'https://twitter.com/oauth/request_token';
  var $access_token_url = 'https://twitter.com/oauth/access_token';
  var $authorize_url = 'https://twitter.com/oauth/authorize';

  function __construct() {
    parent::__construct();

    $hostname = @$_SERVER['HTTP_HOST'] || `uname -n`;
    if (strpos($hostname, '.local') !== FALSE) {
      $this->consumer_key = 'pstfe7cs1vObJfw8tQqoA';
      $this->consumer_secret = '9uvUKjiM8gzhOh8UjR4jGo51jZk8aHsDpceT4Qfo';
    }  
  }


  function initialize(&$controller) {
    parent::initialize($controller);
    $this->authModel = 'TwitterAccount';
  }

  function startUp($controller) {
  }
	
  function request_link() {
    /* Create TwitterOAuth object with app key/secret */
    $to = new TwitterOAuth($this->consumer_key, $this->consumer_secret);
    /* Request tokens from twitter */
    $tok = $to->getRequestToken();

    /* Save tokens for later */
    $_SESSION['oauth_request_token'] = $token = $tok['oauth_token'];
    $_SESSION['oauth_request_token_secret'] = $tok['oauth_token_secret'];

    /* Build the authorization URL */
    return $to->getAuthorizeURL($token);
  }

  function login() {
    /* Create TwitterOAuth object with app key/secret and token key/secret from default phase */
    $to = new TwitterOAuth($this->consumer_key, $this->consumer_secret, $_SESSION['oauth_request_token'], $_SESSION['oauth_request_token_secret']);
    /* Request access tokens from twitter */
    $tok = $to->getAccessToken();

    /* Create TwitterOAuth with app key/secret and user access key/secret */
    $to = new TwitterOAuth($this->consumer_key, $this->consumer_secret, $tok['oauth_token'], $tok['oauth_token_secret']);
    /* Run request on twitter API as user. */
    $account = json_decode($to->OAuthRequest('https://twitter.com/account/verify_credentials.json', array(), 'GET'));

    $account->oauth_access_token = $tok['oauth_token'];
    $account->oauth_access_token_secret = $tok['oauth_token_secret'];

    return $account;

    //$content = $to->OAuthRequest('https://twitter.com/statuses/update.xml', array('status' => 'Test OAuth update. #testoauth'), 'POST');
    //$content = $to->OAuthRequest('https://twitter.com/statuses/replies.xml', array(), 'POST');
  }

  function update_status($status, $token) {
    $to = new TwitterOAuth($this->consumer_key, $this->consumer_secret, $token['oauth_access_token'], $token['oauth_access_token_secret']);
    return json_decode($to->OAuthRequest('https://twitter.com/statuses/update.json', array('status' => $status)));
  }
  
  function search($txt, $token) {
    $to = new TwitterOAuth($this->consumer_key, $this->consumer_secret, $token['oauth_access_token'], $token['oauth_access_token_secret']);
    return json_decode($to->OAuthRequest('http://search.twitter.com/search.json', array('q' => $txt)));
  }
}
